var list = []
var newUsername = ""

function initShopCar(username) {
    newUsername = username
    $.ajaxSettings.async = false;
    $.get("/order?type=shopCar&username=" + username,
        function (data, status) {
            list = JSON.parse(data)
        });
    $.ajaxSettings.async = true;
}


var cartController = function ($scope) {
    $scope.cart = list;
    //总购买数量
    $scope.totalnumber = function () {
        var total = 0;
        angular.forEach($scope.cart, function (item) {
            total += parseInt(item.number);
        });
        return total;
    };
    //总购买价格
    $scope.totalbookPrice = function () {
        var total = 0;
        angular.forEach($scope.cart, function (item) {
            total += parseInt(item.number * item.bookPrice);
        });
        return total;
    };
    //找一个项目
    $scope.findItem = function (id) {
        var index = -1;
        angular.forEach($scope.cart, function (item, key) {
            if (item.id === id) {
                index = key;
                return;
            }
            ;
        });
        return index;
    };
    //移除table
    $scope.remove = function (id) {
        var index = $scope.findItem(id);
        if (index !== -1) {
            $scope.cart.splice(index, 1);
        }
        ;
    };
    //减少一个商品数量
    $scope.reduce = function (id) {
        var index = $scope.findItem(id);
        if (index !== -1) {
            var item = $scope.cart[index]
            if (item.number > 1) {
                --item.number;
                $scope.cart[index].money = $scope.cart[index].bookPrice * $scope.cart[index].number
            } else {
                var returnKey = confirm("是否从购物车中删除该产品！");
                if (returnKey) {
                    $scope.remove(id);
                }
            }
        }
    };
    //增加一个商品数量
    $scope.add = function (id) {
        var index = $scope.findItem(id);
        if (index !== -1) {
            ++$scope.cart[index].number;
            $scope.cart[index].money = $scope.cart[index].bookPrice * $scope.cart[index].number
        }
        ;
    };
    $scope.$watch('cart', function (newvalue, oldvalue) {
        angular.forEach(newvalue, function (item, key) {
            if (item.number < 1 && item.number !== '') {
                var returnKey = confirm("是否从购物车中删除该产品！");
                if (returnKey) {
                    $scope.remove(id);
                } else {
                    item.number = oldvalue[key].number;
                }
                ;
            }
            ;
        });
    }, true);
}

$(function () {
    onSubmitShop = function (username) {
        console.log(list)
        let jsonList = JSON.stringify(list)
        $.ajax({
            url: "/order?type=addOrder&username=" + username,
            data:
                {
                    params: jsonList
                },
            type: "POST",
            traditional: true, //默认false
            success: function (result, status) {
                //do something
                alert(status)
                location.href = "/order?type=userOrderList&username="+username;
            },
            error: function (xhr, status, error) {
                console.log(status)
            }
        });
        // $.post("/order?type=addOrder&username=" + username,
        //     {
        //         mylist: list,
        //     },
        //     function (data, status) {
        //         alert(data)
        //     });
    }
})

